//
//  ACMineViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/29.
//

#import "ACMineViewController.h"
#import "ACMineLogViewController.h"

@interface ACMineViewController() <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UIImageView *headImg;
@property(nonatomic,strong) UILabel *nameLabel;
@property(nonatomic,strong) UILabel *uidLabel;
@property(nonatomic,strong) UITableView *tableview;
@property(nonatomic,strong) UIButton *logoutBtn;
@property(nonatomic,strong) NSMutableArray *titleArray;
@end

@implementation ACMineViewController

-(UIImageView *)headImg{
    if (!_headImg) {
        _headImg = [[UIImageView alloc] initWithFrame:CGRectZero];
        _headImg.layer.masksToBounds = YES;
        _headImg.layer.cornerRadius = 32;
        _headImg.image = [UIImage imageWithName:@"mine_head" bundle:ACLiteAVUISDK targetClass:[self class]];
    }
    return _headImg;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _nameLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
        _nameLabel.textColor = UIColorFromRGB(0x303133);
    }
    return _nameLabel;
}

-(UILabel *)uidLabel{
    if (!_uidLabel) {
        _uidLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _uidLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        _uidLabel.textColor = UIColorFromRGB(0x606266);
    }
    return _uidLabel;
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.rowHeight = 70;
        _tableview.backgroundColor = UIColorFromRGB(0xFFFFFF);
        _tableview.tableFooterView = [UIView new];
        _tableview.scrollEnabled = NO;
    }
    return _tableview;
}

-(UIButton *)logoutBtn{
    if (!_logoutBtn) {
        _logoutBtn = [[UIButton alloc] initWithFrame:CGRectZero];
        _logoutBtn.backgroundColor = UIColorFromRGB(0xFF6048);
        _logoutBtn.layer.masksToBounds = YES;
        _logoutBtn.layer.cornerRadius = 16;
        [_logoutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_logoutBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        _logoutBtn.titleLabel.font = [UIFont systemFontOfSize:20];
        [_logoutBtn setTitle:@"退出" forState:UIControlStateNormal];
        
        [_logoutBtn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColorFromRGB(0xF4F5F8);
    self.title = @"我的";
    
    UIView *mineView = [[UIView alloc] initWithFrame:CGRectZero];
    mineView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mineView];
    [mineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(110);
        make.top.equalTo(self.view).with.offset(10);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    
    [mineView addSubview:self.headImg];
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(64);
        make.width.mas_equalTo(64);
        make.top.equalTo(mineView).with.offset(23);
        make.left.equalTo(mineView).with.offset(15);
    }];
    
    [mineView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(32);
        make.top.equalTo(mineView).with.offset(27);
        make.left.equalTo(self.headImg.mas_right).with.offset(15);
    }];

    [mineView addSubview:self.uidLabel];
    [self.uidLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(32);
        make.bottom.equalTo(mineView).with.offset(-27);
        make.left.equalTo(self.headImg.mas_right).with.offset(15);
    }];
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(280);
        make.top.equalTo(self.view).with.offset(131);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
    }];
    
    [self.view addSubview:self.logoutBtn];
    [self.logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.top.equalTo(self.tableview.mas_bottom).with.offset(30);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
    }];
    
    self.titleArray = [NSMutableArray arrayWithObjects:@"我的直播记录",@"我的会议记录",@"我的点播",@"我的素材库", nil];
    
    if ([[[ACUserProfileManager shareInstance] userModel].avatar isEqualToString:@""]) {
        [[ACUserProfileManager shareInstance] userModel].avatar = @"https://htsec-demo-1302545507.cos.ap-nanjing.myqcloud.com/avatar.png";
    }
    if ([[[ACUserProfileManager shareInstance] userModel].nickname isEqualToString:@""]) {
        NSString *userName = [@"用户_" stringByAppendingString:[[[[ACUserProfileManager shareInstance] userModel].phone componentsSeparatedByString:@"****"] lastObject]];
        [[ACUserProfileManager shareInstance] userModel].nickname = userName;
    }
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:[[ACUserProfileManager shareInstance] userModel].avatar]];
    self.nameLabel.text = [[ACUserProfileManager shareInstance] userModel].nickname;
    self.uidLabel.text = [[ACUserProfileManager shareInstance] userModel].phone;
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MineLogType type = MineLogType_Live;
    switch (indexPath.row) {
        case 0:
            type = MineLogType_Live;
            break;
        case 1:
            type = MineLogType_Meeting;
            break;
        case 2:
            type = MineLogType_Vod;
            break;
        case 3:
            type = MineLogType_File;
            break;
    }
    ACMineLogViewController *logVC = [[ACMineLogViewController alloc] init];
    logVC.type = type;
    [self.navigationController pushViewController:logVC animated:YES];
}

-(void)logout{
    [SVProgressHUD showWithStatus:@""];
    [ACLoginManager requestLogoutWithRequestCallback:^(int code, NSString * _Nullable message) {
        [SVProgressHUD dismiss];
//        跳转到登陆页面
        [self.tabBarController.navigationController popToRootViewControllerAnimated:YES];
    }];
    
}

@end
